from mysql_connect.mysql_db import MysqlDB
from util.runmethod import RunMethod
from util.operation_cookie import OperationCookie
from util.operation_token import OperationToken
from logs.user_log import UserLog
from util.operation_yaml import OperationYaml
from mysql_connect.get_data import GetData
from run_assert.assert_run import AssertRun
import urllib3;urllib3.disable_warnings()
mysql = MysqlDB
run_method = RunMethod()
op_cookie = OperationCookie()
op_token = OperationToken()
log = UserLog().get_log()
op_yaml = OperationYaml()
token_key = 'authenticity_token'
get_data = GetData()
assert_run = AssertRun()


class CookieToken(object):

    def run_assert(self, sql_id, token, cookie, method, url, request_data_dict, headers, header_type, expect, result_key):
        res = self.cookie_token(token, cookie, method, url, request_data_dict, headers, header_type)
        flag, msg = assert_run.assert_main(expect, result_key, res, sql_id=sql_id)
        return flag, msg

    def run_assert_pre(self, sql_id, token, cookie, method, url, request_data_dict, headers, header_type, expect, result_key, case_id):
        res = self.cookie_token(token, cookie, method, url, request_data_dict, headers, header_type)
        pre_assert = assert_run.assert_main(expect, result_key, res, case_id, sql_id=sql_id)
        return pre_assert

    def ct_write(self,token, cookies, method, url, request_data, headers, cookies_dict=None):
        log.debug('token或者cookie为write')
        flag = self.check_cookie()
        res = 'skip'
        if flag is False:
            res = run_method.run_main(method, url, request_data, headers, cookies_dict)
            log.debug(res)
            # log.debug('token：%s, cookies：%s，执行发送请求; res格式为：%s' % (token, cookies,type(res)))
            # 写入token和cookie
            if token is None:
                token = ''
            if cookies is None:
                cookies = ''
            if 'write' in token and 'write' in cookies:
                log.debug('token与cookie都包含“write”，写入新的token和cookie')
                op_token.update_token(res)
                op_cookie.updata_cookie(res)
            # 写入token
            elif 'write' in token:
                log.debug('仅token包含“write”，写入新的token')
                op_token.update_token(res)
            # 写入cookies
            elif 'write' in cookies:
                log.debug('仅cookies包含“write”，写入新的cookies')
                op_cookie.updata_cookie(res)
        return res

    def ct_yes_write(self, token, cookies, method, url, request_data, headers, header_type):
        # 1.yes,获取token或者cookie
        token_as_data, cookies_dict = self.ct_yes(token,cookies, request_data, header_type)
        # 2.write,写入新的token或者cookie
        res = self.ct_write(token, cookies, method, url, token_as_data, headers, cookies_dict)
        return res

    def ct_yes(self, token, cookies, request_dict, header_type):
        # yes,获取token或者cookie
        # 由于只是获取数据，所以token与cookie是独立的，于是直接用了两个if, 分别做两次判断。
        if token is None:
            pass
        elif 'yes' in token:
            log.debug('token包含“yes”，获取token')
            token_value = op_token.get_data()
            request_dict[token_key] = token_value
            log.debug('获取token后的data为：%s' % request_dict)
        if cookies is None:
            pass
        elif 'yes' in cookies:
            log.debug('cookies包含“yes”，获取cookies')
            cookies = op_cookie.get_data()
        token_as_data = request_dict
        cookies_dict = cookies
        return token_as_data, cookies_dict

    def cookie_token(self,token, cookies, method, url, request_data, headers, header_type):
        log.debug('处理cookie与token， "%s", "%s"' % (cookies, token))
        request_data = get_data.get_data_change_type(request_data, header_type)
        # 主函数：判断cookie和token
        if token == 'write' or cookies == 'write':
            res = self.ct_write(token,cookies,method,url,request_data,headers)
            # 发起请求，然后写入cookie和token
        elif token == 'yes_write' or cookies == 'yes_write':
            res = self.ct_yes_write(token, cookies, method, url, request_data, headers, header_type)
            # 获取cookie和token并发起请求，然后写入新的cookie和token
        elif token == 'yes' or cookies == 'yes':
            token_as_data, cookies_dict = self.ct_yes(token, cookies, request_data, header_type)
            res = run_method.run_main(method, url, token_as_data, headers, cookies_dict)
            # 获取cookie和token，然后运行请求
        else:
            # 直接运行请求
            log.debug('运行发送请求; token="%s";cookie="%s"' % (token, cookies))
            res = run_method.run_main(method, url, request_data, headers)
        return res

    def check_cookie(self):
        from run_assert.run_test import RunTest
        log.info('检测cookie是否有效')
        case_id = 'zxyj-07'
        case_data = mysql().get_case_dict_by_CaseID(case_id)
        flag = RunTest().go_on_run(case_dict=case_data)[0]
        if flag:
            log.info('cookie有效；不运行登录用例')
        return flag
